package cn.seaboot.excel;

import cn.seaboot.commons.core.CommonUtils;
import cn.seaboot.commons.digest.Hex;
import cn.seaboot.html.table.TableFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Color;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFColor;
import org.apache.poi.xssf.usermodel.XSSFFont;

import java.util.ArrayList;
import java.util.List;

/**
 * 样式工具
 *
 * @author Mr.css
 * @version 2025-06-26 9:37
 */
public class ExcelStyle {

    /**
     * 获取通用的文本样式信息
     *
     * @param workbook 工作簿 用于获取字体
     * @return -
     */
    public static List<TableFont> getTableFont(Workbook workbook) {
        int len = workbook.getNumberOfFonts();
        List<TableFont> list = new ArrayList<>(len);

        for (int i = 0; i < len; i++) {
            TableFont tableFont = new TableFont();

            org.apache.poi.ss.usermodel.Font font = workbook.getFontAt(i);
            tableFont.setIndex(i);

            // 字体大小
            short fontSize = font.getFontHeightInPoints();
            tableFont.setFontSize(fontSize);

            // 字体名称
            String fontName = font.getFontName();
            tableFont.setFontFamily(fontName);

            // 字体颜色
            if (font.getColor() != XSSFFont.DEFAULT_FONT_COLOR) {
                XSSFFont xssfFont = (XSSFFont) font;
                tableFont.setColor(ExcelStyle.convertToAwtColor(xssfFont.getXSSFColor()));
            }

            // 字体粗细
            if (font.getBold()) {
                tableFont.setFontWeight("bold");
            }

            list.add(tableFont);
        }
        return list;
    }

    /**
     * 获取单元格背景色
     *
     * @param color 色值对象
     * @return 16进制色值
     */
    public static String convertHexString(Color color) {
        if (color instanceof XSSFColor) {
            XSSFColor xssfColor = (XSSFColor) color;

            // 优先使用带亮度信息的色彩
            byte[] rgb = xssfColor.getRGBWithTint();
            if (rgb != null) {
                return Hex.encodeColor(rgb[0], rgb[1], rgb[2]);
            }

            // 次选标准 argb 信息
            byte[] argb = xssfColor.getARGB();
            if (CommonUtils.isEmpty(argb)) {
                return null;
            } else {
                return Hex.encodeColor(argb[1], argb[2], argb[3], argb[0]);
            }
        } else if (color instanceof HSSFColor) {
            return ((HSSFColor) color).getHexString();
        } else {
            throw new RuntimeException("Unsupported color: " + color);
        }
    }

    /**
     * 色值转 jwt 包下的 color 对象
     *
     * @param color poi 包下的色值
     * @return -
     */
    public static java.awt.Color convertToAwtColor(Color color) {
        if (color instanceof XSSFColor) {
            XSSFColor xssfColor = (XSSFColor) color;

            // 优先使用带亮度信息的色彩
            byte[] rgb = xssfColor.getRGBWithTint();
            if (rgb != null) {
                return new java.awt.Color((rgb[0] & 0xFF), (rgb[1] & 0xFF), (rgb[2] & 0xFF));
            }

            // 次选标准 argb 信息
            byte[] argb = xssfColor.getARGB();
            if (CommonUtils.isEmpty(argb)) {
                return null;
            } else {
                return new java.awt.Color((argb[1] & 0xFF), (argb[2] & 0xFF), (argb[3] & 0xFF), (argb[0] & 0xFF));
            }
        } else {
            throw new RuntimeException("Unsupported color: " + color);
        }
    }
}
